//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __STDLIB_H__
#define __STDLIB_H__

#include <types.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

#define RAND_MAX    0x7fff  /* max value returned by rand() function */

#define _MAX_PATH     260     /* max length of full pathname */
#define _MAX_PATH_MB  520     //max length of full pathname for hanzi
#define _MAX_FNAME  256      /* max length of file name compont */

#define _MAX_DRIVE  3       /* max length of drive component */
#define _MAX_DIR       256     /* max length of path component for windows */
#define _MAX_DIR_MB    512     // max length of path component for hanzi
#define _MAX_EXT    256     /* max length of extension component */

#define _MAX_LINE   512

#define EXIT_SUCCESS    0   /* argument values for exit() function */
#define EXIT_FAILURE    1

typedef struct {
    int quot;               /* quotient */
    int rem;                /* remainder */
} div_t;

typedef struct {
    int quot;               /* quotient */
    int rem;                /* remainder */
} ldiv_t;

char *          __cdecl _gcvt(double, int, char *);

int             __cdecl rand(void);
void            __cdecl srand(unsigned int);
char *          __cdecl _ecvt(double, int, int *, int *);
char *          __cdecl _fcvt(double, int, int *, int *);
void            __cdecl exit(int);
void            __cdecl _exit(int);
void            __cdecl abort(void);
int             __cdecl atexit(void (__cdecl *)(void));
void            __cdecl free(void *);
void *          __cdecl malloc(size_t);
void *          __cdecl realloc(void *, size_t);
void *          __cdecl bsearch(const void *, const void *, size_t, size_t,
                            int (__cdecl *)(const void *, const void *));
long            __cdecl wcstol(const wchar_t *, wchar_t **, int);
unsigned long   __cdecl wcstoul(const wchar_t *, wchar_t **, int);
double          __cdecl wcstod(const wchar_t *, wchar_t **);
int             __cdecl _wtoi(const wchar_t *);
wchar_t *       __cdecl _itow(int, wchar_t *, int);
div_t           __cdecl div(int, int);
ldiv_t          __cdecl ldiv(long int, long int);
long            __cdecl _wtol(const wchar_t *);
void            __cdecl _swab(char *, char *, int);
char *          __cdecl getenv(const char *);
int             __cdecl _putenv(const char *);
int             __cdecl system(const char *);

int             __cdecl mblen(const char *, size_t);
int             __cdecl mbtowc(wchar_t *, const char *, size_t);
int             __cdecl wctomb(char *, wchar_t);
size_t          __cdecl mbstowcs(wchar_t *, const char *, size_t);
size_t          __cdecl wcstombs(char *, const wchar_t *, size_t);

//Ascii string to wide char string
size_t          __cdecl _strtowcs(wchar_t *, const char *);
size_t          __cdecl _wcstostr(char *, const wchar_t *);

/* Compiler intrinsics */
unsigned long   __cdecl _lrotl(unsigned long, int);
unsigned long   __cdecl _lrotr(unsigned long, int);
unsigned int    __cdecl _rotl(unsigned int, int);
unsigned int    __cdecl _rotr(unsigned int, int);

long            __cdecl strtol(const char *, char **, int);
unsigned long   __cdecl strtoul(const char *, char **, int);
double          __cdecl strtod(const char *, char **);
double          __cdecl atof(const char *);
int             __cdecl atoi(const char *);
long            __cdecl atol(const char *);
char *          __cdecl _ultoa(unsigned long, char *, int);
wchar_t *       __cdecl _ultow (unsigned long, wchar_t *, int);

__uint64        __cdecl _strtoui64(const char *, char **, int);
__uint64        __cdecl _wcstoui64(const wchar_t *, wchar_t **, int);
__int64         __cdecl _strtoi64(const char *, char **, int);
__int64         __cdecl _wcstoi64(const wchar_t *, wchar_t **, int);

char *          __cdecl _itoa(int, char *, int);
char *          __cdecl _ltoa(long, char *, int);
wchar_t *       __cdecl _ltow(long, wchar_t *, int);

char *          __cdecl _i64toa(__int64, char *, int);
wchar_t *       __cdecl _i64tow(__int64, wchar_t *, int);
char *          __cdecl _ui64toa(__uint64, char *, int);
wchar_t *       __cdecl _ui64tow(__uint64, wchar_t *, int);
__int64           __cdecl _atoi64(const char *);
__int64           __cdecl _wtoi64(const wchar_t *);

void            __cdecl qsort( void *base, size_t num, size_t width, \
                int(__cdecl *compare)(const void *elem1, const void *elem2));
void *          __cdecl calloc(size_t num, size_t size);

wchar_t *       __cdecl _wgetenv(const wchar_t *);
int             __cdecl _wputenv(const wchar_t *);
int             __cdecl _wsystem(const wchar_t *);
char *          __cdecl _fullpath(char *, const char *, size_t);
wchar_t *       __cdecl _wfullpath(wchar_t *, const wchar_t *, int);

void            __cdecl _makepath(char*, const char*, const char*,
                                const char*, const char*);
void            __cdecl _splitpath(const char*, char*, char*, char*, char*);
void            __cdecl _wmakepath(wchar_t*, const wchar_t*, const wchar_t*,
                                const wchar_t*, const wchar_t*);
void            __cdecl _wsplitpath(const wchar_t*, wchar_t*, wchar_t*,
                                wchar_t*, wchar_t*);

void            __cdecl _searchenv( const char *filename,
                                const char *varname, char *pathname );
void            __cdecl _wsearchenv( const wchar_t *filename,
                                const wchar_t *varname, wchar_t *pathname );

//int             __cdecl _gbstowcs(wchar_t *outptr, size_t *pOutSize,
//                                    const char *inptr, size_t *pInSize);
//int             __cdecl _wcstogbs(char *outptr, size_t *pOutSize,
//                                    const wchar_t *inptr, size_t *pInSize);

int * __cdecl __p___mb_cur_max(void);
#define MB_CUR_MAX (*__p___mb_cur_max())

int* __cdecl __p__fmode(void);
#define _fmode (*__p__fmode())

char ***       __cdecl __p__environ(void);
wchar_t ***    __cdecl __p__wenviron(void);
#define _environ    (*__p__environ())   // pointer to environment table
#define _wenviron   (*__p__wenviron())  // pointer to wide environment table

//POSIX
char *          __cdecl ecvt(double, int, int *, int *);
char *          __cdecl fcvt(double, int, int *, int *);
void            __cdecl swab(char *, char *, int);
int             __cdecl putenv(const char *);
char *          __cdecl ultoa(unsigned long, char *, int);
char *          __cdecl itoa(int, char *, int);
char *          __cdecl ltoa(long, char *, int);

#ifndef _GNUC
#pragma function(_lrotl, _lrotr, _rotl, _rotr)
#endif

#ifdef __cplusplus
}
#endif

#endif //__STDLIB_H__
